package Presenter;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import Models.BoilHops;

public class BoilHopsPresenter extends AbstractPresenter<BoilHops> {
	final String SINGLEITEM = "boilHops";
	final String MULTIITEM = "boilHopss";
	
	public ArrayList<BoilHops> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public BoilHops tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public BoilHops createItemFromJSONObject(JSONObject jsonObject){
		BoilHops item = new BoilHops();
		try{
			item.setId(jsonObject.getInt("boilHopsID"));
			item.setLbs(jsonObject.getDouble("boilHopLbs"));
			item.setMinutes(jsonObject.getDouble("boilHopMinutes"));
			item.setFirstWortHop(jsonObject.getBoolean("boilHopFWH"));
			
			final HopsPresenter presenter = new HopsPresenter();
			JSONObject jsonSC = jsonObject.getJSONObject("hopID");
			item.setHop(presenter.createItemFromJSONObject(jsonSC));
			
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(BoilHops item){
		try{
			JSONObject json = new JSONObject();
			json.put("boilHopsID", item.getId());
			json.put("boilHopLbs", item.getLbs());
			json.put("boilHopMinutes", item.getMinutes());
			json.put("boilHopFWH", item.isFirstWortHop());
			
			final HopsPresenter presenter = new HopsPresenter();
			json.put("hopID", presenter.createJSON(item.getHop()));

			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public BoilHops getEmptyEntity(){
		BoilHops entity = new BoilHops();
		return entity;
	}
}
